﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using BUS;
using DAO;
using DTO;
namespace baitaptuan5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            sachbus = new SachBUS();
            sachdto = new SachDTO();
            tgbus = new TacGiaBUS();
            tgdto = new TacGiaDTO();
            tlbus = new TheloaiBUS();
            tldto = new TheLoaiDTO();
            dtSach = sachbus.laybang();
            dtTacGia = tgbus.laybang();
            dtTheLoai = tlbus.laybang();
           
            check = 0;
            loadGritVIew();
            loadCombobox();
            dem();
        }
        SachBUS sachbus;
        SachDTO sachdto;
        TacGiaBUS tgbus;
        TacGiaDTO tgdto;
        TheloaiBUS tlbus;
        TheLoaiDTO tldto;
        DataTable dtSach,dtTheLoai,dtTacGia;
        int check;
        int idem = 0;
        void dem()
        {
            idem = 0;
            dataGridView1.DataSource = dtSach;
            foreach (DataRow row in dtSach.Rows)
            {
                idem++;
            }
            toolStripStatusLabel2.Text = idem.ToString();
        }
        public void loadGritVIew()
        {
            dtSach = sachbus.laybang();
            dataGridView1.DataSource = dtSach;
        }
        void loadCombobox()
        {
            
            comboBox1.DataSource = dtTheLoai;
            comboBox1.DisplayMember = "TenTheLoai";
            comboBox1.ValueMember = "MaTheLoai";
            comboBox2.DataSource = dtTacGia;
            comboBox2.DisplayMember = "TenTacGia";
            comboBox2.ValueMember = "MaTG";
            check = 1;
        }
        void timkiem()
        {

                dtSach = sachbus.laybangLoaivaTG(int.Parse(comboBox1.SelectedValue.ToString()), int.Parse(comboBox2.SelectedValue.ToString()));
                dataGridView1.DataSource = dtSach;
            
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (check == 1)
            {
                dtSach = sachbus.laybangLoai(int.Parse(comboBox1.SelectedValue.ToString()));
                dataGridView1.DataSource = dtSach;
            }
            if (checkBox1.Checked == true)
            {
                timkiem();
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (check == 1)
            {
                dtSach = sachbus.laybangTG(int.Parse(comboBox2.SelectedValue.ToString()));
                dataGridView1.DataSource = dtSach;
            }
            if (checkBox1.Checked == true)
            {
                timkiem();
                
            }
        }
        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                int r = this.dataGridView1.CurrentCell.RowIndex;
                sachdto.Masach = int.Parse(dataGridView1.Rows[r].Cells[0].Value.ToString());
                sachdto.Tensach = dataGridView1.Rows[r].Cells[1].Value.ToString();
                sachdto.Matacgia = int.Parse(dataGridView1.Rows[r].Cells[4].Value.ToString());
                sachdto.Matheloai = int.Parse(dataGridView1.Rows[r].Cells[2].Value.ToString());
                sachdto.DonGia = int.Parse(dataGridView1.Rows[r].Cells[5].Value.ToString());
                sachdto.Soluong = int.Parse(dataGridView1.Rows[r].Cells[3].Value.ToString());
            }
            catch (Exception)
            {
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                sachdto = new SachDTO();
            ThemSach them = new ThemSach(sachdto);
            them.ShowDialog();
            }
            catch (Exception)
            {
                MessageBox.Show("XIN THỬ LẠI", "THÔNG BÁO");
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult result = MessageBox.Show("CÓ THỰC SỰ MUỐN XÓA KHÔNG", "THÔNG BÁO", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    sachbus.xoa(sachdto);
                    MessageBox.Show("ĐÃ XÓA THÀNH CÔNG", "THÔNG BÁO");
                    dem();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("XIN THỬ LẠI", "THÔNG BÁO");
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
             try
            {
            ThemSach them = new ThemSach(sachdto);
            them.ShowDialog();
            loadGritVIew();
            }
             catch (Exception)
             {
                 MessageBox.Show("XIN THỬ LẠI", "THÔNG BÁO");
             }
        }


        private void Form1_Shown(object sender, EventArgs e)
        {
            try
            {
                loadGritVIew();
                MessageBox.Show("ĐÃ CẬP NHẬT LẠI DANH SÁCH DỮ LIỆU", "THÔNG BÁO");
            }
            catch (Exception)
            {
                MessageBox.Show("XIN THỬ LẠI", "THÔNG BÁO");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
